//
//  HRFunChatViewController.m
//  Hear
//
//  Created by mac on 2017/8/4.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRFunChatViewController.h"
#import "HRFunChatCell.h"
#import "HRFunChatViewModel.h"
#import "MJRefresh.h"
#import "HRFunChatModel.h"
#import "HRSearchFunChatFriendViewController.h"
#import "HRFunChatFrameModel.h"
#import "HRLifeDetailViewController.h"
#import "TableViewAnimationKitHeaders.h"

@interface HRFunChatViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)HRFunChatViewModel *viewModel;
@property (nonatomic, assign)NSInteger pIndex;
@property(nonatomic, strong)NSMutableArray *colorContent;
@property(nonatomic, strong)NSMutableArray *contentArray;

@end

@implementation HRFunChatViewController
#pragma mark - Get and Set
- (NSMutableArray *)contentArray{
    if (_contentArray == nil) {
        _contentArray = [[NSMutableArray alloc] init];
    }
    return _contentArray;
}

- (HRFunChatViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[HRFunChatViewModel alloc] init];
    }
    return _viewModel;
}

- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
        _tableView.frame = self.view.bounds;
    }
    return _tableView;
}




- (void)viewDidLoad {
    [super viewDidLoad];    
    self.colorContent = [NSMutableArray arrayWithObjects:CustomColor(255, 170, 190, 1),CustomColor(255, 212, 125, 1),CustomColor(113, 233, 171, 1),CustomColor(255, 174, 255, 1),CustomColor(117, 234, 239, 1),CustomColor(255, 146, 149, 1), nil];
    [self getFunChatContent];
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HRFunChatFrameModel *model = self.contentArray[indexPath.row];
    return model.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HRFunChatCell *cell = [HRFunChatCell cellWithTableView:tableView];
    HRFunChatFrameModel *model = self.contentArray[indexPath.row];
    cell.funChatFrameModel = model;
    cell.backCorlor = self.colorContent[indexPath.row%self.colorContent.count];
    return cell;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRFunChatFrameModel *model = self.contentArray[indexPath.row];
    if (!model)return;
    if ([model.funModel.LocalId isEqualToString:@"TheSims"]){
        HRLifeDetailViewController *VC = [HRLifeDetailViewController new];
        VC.TopicLocalId = model.funModel.LocalId;
        [self.navigationController pushViewController:VC animated:YES];
    }else{
        HRSearchFunChatFriendViewController *searchVC = [HRSearchFunChatFriendViewController new];
        searchVC.TopicTitle = model.funModel.TopicTitle;
        searchVC.TopicLocalId = model.funModel.LocalId;
        [self.navigationController pushViewController:searchVC animated:YES];
    }

}

- (void)starAnimationWithTableView:(UITableView *)tableView {
    
    [TableViewAnimationKit showWithAnimationType:4 tableView:tableView];
}


- (void)getFunChatContent{
    self.pIndex = 0;
    
    [self getFunChatData:YES];

}

- (void)getFunChatData:(BOOL)isHead{
    WeakSelf
    [self.viewModel getTopicsContent:nil contentArr:^(NSMutableArray *contentArr) {
        if (contentArr){
//            if (isHead){
//                [weakSelf.contentArray removeAllObjects];
//            }
            [weakSelf.contentArray addObjectsFromArray:contentArr];

//            HRFunChatModel *chatModel = [HRFunChatModel new];
//            chatModel.LocalId = @"TheSims";
//            chatModel.TopicTitle = @"特殊话题-Hearer模拟人生双人游戏";
//            chatModel.OnlineCount = @"0";
//            chatModel.CreateTime = [self getCurrentTime];
//            HRFunChatFrameModel *frameModel = [HRFunChatFrameModel new];
//            frameModel.funModel = chatModel;
//            [weakSelf.contentArray insertObject:frameModel atIndex:contentArr.count];

            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
                if (weakSelf.contentArray.count){
                    [weakSelf starAnimationWithTableView:self.tableView];
                }
            });
        }
    }];
}

- (NSString *)getCurrentTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    
    return [formatter stringFromDate:[NSDate date]];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
